﻿using BPMS.Utility.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace BPMS.Web.Extends.Attributes
{
    public class CustomExceptionAttribute : HandleErrorAttribute, IExceptionFilter
    {
        public override void OnException(ExceptionContext filterContext)
        {
            if (null == filterContext)
            {
                return;
            }
            HttpException httpException = filterContext.Exception as HttpException;
            if (httpException == null)
            {
                //TODO 记录日志
                filterContext.ExceptionHandled = true;
                return;
            }
            if (filterContext.ExceptionHandled == true)
            {
                if (httpException.GetHttpCode() != 500)
                {
                    return;
                }
            }

            filterContext.Controller.ViewBag.UrlRefer = filterContext.HttpContext.Request.UrlReferrer;
            if (httpException.GetHttpCode() == 404)
            {
                filterContext.Result = new RedirectResult(URLUtility.RootUrl + "/error/notfound");
            }
            else if (httpException.GetHttpCode() == 500)
            {
                filterContext.Result = new RedirectResult(URLUtility.RootUrl + "/error");
            }
            filterContext.ExceptionHandled = true;
        }
    }
}
